package com.example.inter.tourdemo.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.inter.tourdemo.R;
import com.example.inter.tourdemo.ui.GlideImageLoader;
import com.example.inter.tourdemo.utils.MyApplication;
import com.youth.banner.Banner;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by Inter巩海亮 on 2017-03-28.
 * 微信:
 * QQ号:
 * 邮箱:dahaizhiliang@126.com
 */

public class ThemeFragment extends SupportFragment {

    public static ThemeFragment newInstance() {
        Bundle args = new Bundle();
        ThemeFragment fragment = new ThemeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private View view;

    //1.初始化控件
    private RecyclerView theme_rv_channel;
    private RecyclerView theme_rv_Boutique;
    //2.使用常用适配器
    private CommonAdapter<String> mAdapter;
    private CommonAdapter<String> mAdapterjp;

    //    private TextView textview_gd;
    //3. 创建集合
    private List<String> mDatas = new ArrayList<>();

    private List<String> pdDatas = new ArrayList<>();



    //第一步
    private Banner banner;

    //第二步
    Integer[] images = {R.drawable.banner01, R.drawable.banner02, R.drawable.banner03};

    //★轮播图实现

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_old, container, false);

        //频道执行
        theme_rv_channel = (RecyclerView) view.findViewById(R.id.theme_rv_channel);

        //精品推荐
        theme_rv_Boutique = (RecyclerView) view.findViewById(R.id.theme_rv_Boutique);


        initDatas();

        //第一步
        banner = (Banner)view.findViewById(R.id.banner);


        //第三步:测试 本地图片数据（资源文件）
        List list = new ArrayList();
        for (int i = 0; i < images.length; i++) {
            list.add(images[i]);
        }
        banner.setImages(list)
                .setImageLoader(new GlideImageLoader())
                .start();


        //精品: 把item的布局放到总布局中,同时把数据添加进去
        mAdapterjp = new CommonAdapter<String>(MyApplication.getContext(), R.layout.item_list, mDatas) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                holder.setText(R.id.activity_item_tv, mDatas.get(position));
            }
        };

        //频道:把item的布局放到总布局中,同时把数据添加进去
        mAdapter = new CommonAdapter<String>(MyApplication.getContext(), R.layout.item_list_channel, pdDatas) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                holder.setText(R.id.channel_tv, pdDatas.get(position));
            }
        };

        //频道
        theme_rv_channel.setAdapter(mAdapter);

        //精品
        theme_rv_Boutique.setAdapter(mAdapterjp);

        theme_rv_Boutique.addItemDecoration(new com.example.inter.tourdemo.utils.DividerItemDecoration(MyApplication.getContext(), com.example.inter.tourdemo.utils.DividerItemDecoration.VERTICAL_LIST));

        //为了解决NestedScrollview嵌套Recyclerview滑动缓慢故加的代码
        LinearLayoutManager layoutManager = new LinearLayoutManager(MyApplication.getContext());
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);

        theme_rv_Boutique.setLayoutManager(layoutManager);
        theme_rv_Boutique.setHasFixedSize(true);
        theme_rv_Boutique.setNestedScrollingEnabled(false);
        //频道
        StaggeredGridLayoutManager layoutGrid = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.HORIZONTAL);
//        layoutGrid.sets
//        layoutGrid.setSmoothScrollbarEnabled(true);
        layoutGrid.setAutoMeasureEnabled(true);

        theme_rv_channel.setLayoutManager(layoutGrid);
        theme_rv_channel.setHasFixedSize(true);
        theme_rv_channel.setNestedScrollingEnabled(false);

//        theme_rv_channel.setLayoutManager(new StaggeredGridLayoutManager(2,
//                StaggeredGridLayoutManager.HORIZONTAL));
        return view;
    }

    //初始数据
    private void initDatas() {
        for (int i = 'A'; i <= 'z'; i++) {
            mDatas.add((char) i + "");
        }


        pdDatas.add("景点讲解");
        pdDatas.add("主题游");
        pdDatas.add("自驾游");
        pdDatas.add("当地游");
        pdDatas.add("境外游");
        pdDatas.add("游客秀");
        pdDatas.add("亲子");
        pdDatas.add("跟团游");
        pdDatas.add("登山");
        pdDatas.add("度假村");
        pdDatas.add("景点讲解");
        pdDatas.add("主题游");
        pdDatas.add("自驾游");
        pdDatas.add("当地游");
        pdDatas.add("境外游");
        pdDatas.add("游客秀");
        pdDatas.add("亲子");
        pdDatas.add("跟团游");
        pdDatas.add("登山");
        pdDatas.add("度假村");


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
